package com.my.hotel.controller.liuhao;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.my.hotel.entity.*;
import com.my.hotel.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/eva",produces = "text/html;charset=utf-8")
public class CEvaController {
    @Autowired
    UserService userService;
    @Autowired
    KfRoomService kfRoomService;
    @Autowired
    KfImgService kfImgService;
    @Autowired
    EvaService evaService;
    @Autowired
    EvaImgService evaImgService;


    @RequestMapping("/eva_tiaojian")
    public String eva_tiaojian(Model model,String pj,String px,String pageNo) {
        try {
            if (pageNo==null){
                pageNo="1";
            }
            //对用户进行全查询
            List<User> userList = userService.getList();
            //对客房进行全查询
            List<KfRoom> kfRoomList = kfRoomService.getList();
            //对评论图片进行全查询
            /*List<KfImg> kfImgList = kfImgService.selectAllKfImg();*/
            List<EvaImg> evaImgList = evaImgService.evaImg_All();
            //查询好评数
            ArrayList<String> hp_list = new ArrayList<String>();
            hp_list.add("5");
            int hp_num = evaService.eva_hzc_num(hp_list);
            //查询好评数
            ArrayList<String> zp_list = new ArrayList<String>();
            zp_list.add("2");
            zp_list.add("3");
            zp_list.add("4");
            int zp_num = evaService.eva_hzc_num(zp_list);
            //查询好评数
            ArrayList<String> cp_list = new ArrayList<String>();
            cp_list.add("1");
            int cp_num = evaService.eva_hzc_num(cp_list);
            model.addAttribute("hp_num",hp_num);
            model.addAttribute("zp_num",zp_num);
            model.addAttribute("cp_num",cp_num);


            PageHelper.startPage(Integer.valueOf(pageNo),2);

            ArrayList<String> list = new ArrayList<String>();
            if (pj.equals("全部")){
                list.add("1");
                list.add("2");
                list.add("3");
                list.add("4");
                list.add("5");
            }else if(pj.equals("好评")){
                list.add("5");
            }else if (pj.equals("中评")){
                list.add("2");
                list.add("3");
                list.add("4");
            }else if (pj.equals("差评")){
                list.add("1");
            }
            List<Eva> evas=new ArrayList();
            if (px.equals("热门排序")){
                evas=evaService.eva_rm_px(list);
            }else {
                 evas = evaService.eva_pj(list);
            }

            for (Eva eva1:evas){
                String evaTime_str = eva1.getEvaTime();
                SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                SimpleDateFormat format2 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
                Date evaTime = format1.parse(evaTime_str);
                String evaTime_str2 = format2.format(evaTime);
                eva1.setEvaTime(evaTime_str2);
            }
            PageInfo<Eva> evaPageInfo = new PageInfo<Eva>(evas);

            model.addAttribute("userList",userList);
            model.addAttribute("kfRoomList",kfRoomList);
            model.addAttribute("evaImgList",evaImgList);
            model.addAttribute("evaPageInfo",evaPageInfo);
            model.addAttribute("pj",pj);
            model.addAttribute("px",px);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "forward:/index.jsp";
    }

    @RequestMapping("/pre_eva_reply")
    public String pre_eva_reply(HttpServletRequest request,String id){
        request.setAttribute("evaId",id);

        //对回评进行添加查询条件为evaId
        List<EvaReply> evaReplyList = evaService.selectEvaReplyByEvaId(Long.valueOf(id));
        request.setAttribute("evaReplyList",evaReplyList);
        return "forward:/eva_reply.jsp";
    }

    @RequestMapping("/eva_reply")
    public String eva_reply(HttpServletRequest request, EvaReply evaReply){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        evaReply.setReplyTime(format.format(new Date()));
        evaService.eva_reply(evaReply);
        return "forward:/kfRoom/kfRoomyd";
    }

    @RequestMapping("eva_dz")
    @ResponseBody
    public String eva_dz(String evaId){
        //修改点赞数
        evaService.eva_dz_num(evaId);
        //查询点赞评论的点赞数
        Eva eva = evaService.eva_Id(evaId);
        String eva_str = JSONObject.toJSONString(eva);
        return eva_str;
    }
}
